package cn.edu.ctbu.studentstest.service.impl;

import cn.edu.ctbu.studentstest.dao.StudentRepository;
import cn.edu.ctbu.studentstest.domain.Students;
import cn.edu.ctbu.studentstest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentimplService implements StudentService {
    public List<Students> findByStudentName;
    @Autowired
    StudentRepository studentRepository;
/**
*@return访问所有数据
 **/
    public List<Students>findAll(){

        return studentRepository.findAll();
    }
    /**
     * 分页取出所有数据
     * @param pageable
     * @return
     */
    public Page<Students> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }

    /**
     * 分页,带过滤的查询
     * @param pageable
     * @return
     */
    public Page<Students> findAll(Example<Students> student, Pageable pageable) {
        return studentRepository.findAll(student,pageable);
    }
    /**
     * 插入学生对象
     */
    public Students insert(Students students){

        return studentRepository.save(students);
    }
    /**
     * 更新学生对象
     */
    public Students upData(Students students){

        return studentRepository.save(students);
    }
    /**
     * 删除学生
     */
    public void delete(Students students){

         studentRepository.delete(students);
    }
    public  void delete2(Long id){
        Students student=new Students();
        student.setId(id);

        studentRepository.delete(student);
    }
    /**
     * 根据id来查询学生
     */
    public Students getById(Long id){
        Students students = studentRepository.findById(id).orElse(null);
        return students;
    }
    /**
     * 根据姓名来查询学生
     */
    public List<Students>findByStudentName(String name)
    {
        return studentRepository.findByStudentName(name);
    }

    /**
     * 根据id来删除学生
     */
    public void delete(Long id) {
        studentRepository.deleteById(id);
    }
}
